/*
 * Copyright (C) 2012 VinaVAC. All rights reserved.
 */
package org.vinavac.mob.converter;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.vinavac.entity.StaffRole;
import org.vinavac.mob.mbean.StaffRoleMobileBean;

/**
 *
 * @author Quang <quangtin3@gmail.com>
 */
@FacesConverter(forClass = StaffRole.class)
public class StaffRoleMobileConverter extends AbstractMobileConverter implements Converter {

    private static final Logger logger = Logger.getLogger(StaffRoleMobileConverter.class.getName());

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        try {
            StaffRoleMobileBean controller = (StaffRoleMobileBean) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "staffRoleMobileBean");
            return controller.find(getKey(value));
        } catch (Exception e) {
            logger.log(Level.WARNING, "StaffRole converter error", e);
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof StaffRole) {
            StaffRole o = (StaffRole) object;
            return getStringKey(o.getId());
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + StaffRoleMobileBean.class.getName());
        }
    }
}
